package com.example.xd.defaultcall;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import java.io.File;
import java.io.IOException;

/**
 * @auther XuDan on 2020/6/8.
 */
public class RecordingService extends Service {

    private MediaRecorder mMediaRecorder;
    String path = Environment.getExternalStorageDirectory().getPath() + "/default/record/";
    private long mTimeMillis;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        startRecord();
        Log.i("xd-----", "开启录音");
        return super.onStartCommand(intent, flags, startId);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void startRecord() {
        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        mTimeMillis = System.currentTimeMillis();
        File file = new File(path + mTimeMillis + ".mp3");
        mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        mMediaRecorder.setOutputFile(file);
        try {
            mMediaRecorder.prepare();
            mMediaRecorder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("xd-----", "关闭录音");
        if (mMediaRecorder != null) {
            stopRecord();
        }
        sendNotification();

    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void sendNotification() {
        String channelId = "channelId";
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationChannel notificationChannel = new NotificationChannel(channelId, "default",
                NotificationManager.IMPORTANCE_DEFAULT);
        mNotificationManager.createNotificationChannel(notificationChannel);
        Intent intent = new Intent(this, RecordNotificationActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("path", path + mTimeMillis + ".mp3");
        PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("录音")
                .setContentText(".mp3")
                .setChannelId(channelId)
                .setContentIntent(activity)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.volume)
                .setAutoCancel(true)
                .build();
        mNotificationManager.notify(1, notification);

    }

    private void stopRecord() {
        mMediaRecorder.stop();
        mMediaRecorder.release();
        mMediaRecorder = null;
    }
}
